﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoPartsSearch.Domain.Abstract;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoPartsSearch.Domain.Entities;
using AutoPartsSearch.Web.ViewModels.SearchQuery;
using AutoMapper;
using AutoPartsSearch.Web.ViewModels;

namespace AutoPartsSearch.Web.Controllers
{
    public class SearchQueryController : Controller
    {
        private ISearchQueryRepository _repository;
        private ISearchQueryProcessor _processor;

        public SearchQueryController(ISearchQueryRepository repository, ISearchQueryProcessor processor)
        {
            this._repository = repository;
            this._processor = processor;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Query()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Query(SearchQueryAdd viewSearchQuery)
        {
            if (ModelState.IsValid)
            {
                var parts = viewSearchQuery.Parts.ToList();
                if (!string.IsNullOrEmpty(viewSearchQuery.CarModelAnother))
                {
                    viewSearchQuery.CarModel = viewSearchQuery.CarModelAnother;
                }
                var searchQueries = Mapper.Map<List<SearchQuery>>(viewSearchQuery);
                this._processor.Process(searchQueries,
                                        i => Url.Action("Details", "SellerSearchQuery", new {id = i}, "http"));
                return RedirectToAction("Confirmed");
            }
            return View(viewSearchQuery);
        }

        public ViewResult List(int page = 1, int pageSize = 3)
        {
            if (page < 1)
            {
                throw new ArgumentOutOfRangeException("page", "Page cannot be less than 1.");
            }
            if (pageSize < 1)
            {
                throw new ArgumentOutOfRangeException("pageSize", "PageSize cannot be less than 1.");
            }
            var totalCount = this._repository.GetTotalCount();
            var maxPage = Math.Ceiling((double)totalCount / pageSize);
            if (page > maxPage)
            {
                throw new ArgumentOutOfRangeException("page", "Page cannot be more than maximum page");
            }
            var pagingInfo = new PagingInfo
                                 {
                                     CurrentPage = page,
                                     ItemsPerPage = pageSize,
                                     TotalItems = totalCount
                                 };
            var resut = this._repository.Get(page, pageSize);
            return View(new SearchQueryView
                            {
                                PagingInfo = pagingInfo,
                                Items = resut
                            });
        }

        public PartialViewResult PartialList(int count = 10)
        {
            var result = this._repository.Get(1, count);
            return PartialView(result);
        }

        public ActionResult Confirmed()
        {
            return View();
        }
    }
}
